package myclient;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
/**
 * @author JIAO
 */
public class LoginFrame{

	private JTextField textField;
	private JTextField textFieldName;
	private JFrame frame;
	private String ip;
	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String args[]) {
		new GetIP();
		
	}

	/**
	 * Create the frame
	 */
	public LoginFrame(final String ip) {
		this.ip = ip;
		frame = new JFrame();
		frame.getContentPane().setLayout(null);
		frame.getContentPane().setBackground(new Color(0,200,255));
		frame.setBounds(100, 100, 334, 299);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		textFieldName = new JTextField();
		textFieldName.setBounds(65, 105, 186, 25);
		frame.getContentPane().add(textFieldName);

		textField = new JTextField();
		textField.setBounds(65, 160, 186, 25);
		frame.getContentPane().add(textField);

		final JLabel label = new JLabel();
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setHorizontalTextPosition(SwingConstants.RIGHT);
		label.setText("账号：");
		label.setBounds(0, 105, 57, 25);
		frame.getContentPane().add(label);

		final JLabel label_1 = new JLabel();
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setText("密码：");
		label_1.setBounds(0, 163, 59, 18);
		frame.getContentPane().add(label_1);

		final JLabel label_2 = new JLabel();
		label_2.setHorizontalAlignment(SwingConstants.LEFT);
		label_2.setText("注册新账号");
		label_2.setBounds(260, 105, 66, 25);
		frame.getContentPane().add(label_2);
		label_2.addMouseListener(new MouseAdapter(){
			public void mouseEntered(MouseEvent e){
				label_2.setForeground(Color.blue);
			}
			
			public void mouseExited(MouseEvent e){
				label_2.setForeground(Color.black);
			}
			
			public void mouseClicked(MouseEvent e){
				new RegisterFrame();
			}
		});

		final JButton button = new JButton();
		button.setText("设置");
		button.setBounds(10, 227, 106, 28);
		frame.getContentPane().add(button);

		final JButton button_1 = new JButton();
		button_1.setText("登陆");
		button_1.setBounds(210, 227, 106, 28);
		frame.getContentPane().add(button_1);
		button_1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				
				String id = textFieldName.getText().trim();
				String strReturn = getLogin(id,textField.getText().trim());
				
				if(strReturn.length()>2){//返回值为端口号
					frame.dispose();
					new MainFrame(ip,id,strReturn);
					frameClose();
				}
			}
		});
		frame.setVisible(true);
	}
	
	private void frameClose(){
		System.out.println("JFrame.DISPOSE_ON_CLOSE");
		frame.dispose();
//		WindowEvent.dispose();
//		new WindowEvent();
		
	}
	
	/**
	 * 登录方法，入参为  名字 和密码
	 * @return String -1 0 端口号
	 * 
	 */
	private String getLogin(String sName,String sMima){
		Socket s;
		String strReturn="";
		try {
			s = new Socket(ip,9001);
			PrintWriter writer = new PrintWriter(s.getOutputStream(),true);
			writer.println(sName);
			writer.println(sMima);
			BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
			String hasName = reader.readLine();
			if(hasName.equals("-1")){//如果没有返回has说明，用户没有注册
				JOptionPane.showMessageDialog(null,"您没有注册，请先注册");
				strReturn = hasName;
			}else if(hasName.equals("0")){
				JOptionPane.showMessageDialog(null,"您的密码不正确，请重新登录！");
				strReturn = hasName;
			}else if(hasName.equals("2")){
				JOptionPane.showMessageDialog(null,"您的账号已经登录！");
				strReturn = hasName;
			}else{
				strReturn = reader.readLine();
			}
			s.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return strReturn;
		
	}
}
